/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jesseenglish.swingftfy.additions;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.LinkedList;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author jesse
 */
public class AccordionJPanel extends JPanel {

    public static int DISPLAY_MODE_SINGLE = 1;
    public static int DISPLAY_MODE_MULTIPLE = 2;
    
    private static ImageIcon EXPAND_ICON = new ImageIcon(AccordionJPanel.class.getResource("/images/expand.png"));
    private static ImageIcon COLLAPSE_ICON = new ImageIcon(AccordionJPanel.class.getResource("/images/collapse.png"));
    
    
    private LinkedList<AccordionContent> contents;
    private int displayMode;
    private LinkedList<AccordionContent> displayed;
    
    private Color titleBackground;
    private Color titleHover;
    private Color titleSelected;
    
    public AccordionJPanel() {
        this.contents = new LinkedList();
        this.displayMode = DISPLAY_MODE_SINGLE;
        this.displayed = new LinkedList();
        
        this.titleBackground = new Color(0.8f, 0.8f, 0.9f);
        this.titleHover = new Color(0.9f, 0.9f, 1.0f);
        this.titleSelected = new Color(0.85f, 0.85f, 0.95f);
        
        refresh();
    }
    
    public void display(String title) {
        for (AccordionContent content : contents) {
            if (title.equalsIgnoreCase(content.title)) {
                displayed.add(content);
            }
        }
        refresh();
    }
    
    private void refresh() {
        this.removeAll();
        this.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.gridy = -1;
        
        for (AccordionContent content : contents) {
            String title = content.title;
            JPanel panel = content.content;
            
            TitleJPanel titlePanel = new TitleJPanel(content);
            
            c.gridx = 0;
            c.gridy++;
            c.weightx = 0.1;
            c.weighty = 0.0;
            c.fill = GridBagConstraints.BOTH;
            c.insets = new Insets(1, 1, 1, 1);
            
            this.add(titlePanel, c);
            
            c.gridx = 0;
            c.gridy++;
            c.weightx = 0.1;
            c.weighty = 0.0;
            c.fill = GridBagConstraints.BOTH;
            c.insets = new Insets(0, 0, 0, 0);
            
            if (this.displayed.contains(content)) {
                this.add(panel, c);
            }
        }
        
        JPanel filler = new JPanel();
        c.gridx = 0;
        c.gridy++;
        c.weightx = 0.1;
        c.weighty = 0.1;
        c.fill = GridBagConstraints.BOTH;
        
        this.add(filler, c);
        
        this.validate();
        this.repaint();
    }
    
    public void add(String title, JPanel panel) {
        this.contents.add(new AccordionContent(title, panel));
        refresh();
    }

    public void setDisplayMode(int displayMode) {
        this.displayMode = displayMode;
    }
    
    private class AccordionContent {
        public String title;
        public JPanel content;

        public AccordionContent(String title, JPanel content) {
            this.title = title;
            this.content = content;
        }
    }
    
    private class TitleJPanel extends JPanel implements MouseListener, MouseMotionListener {
        private AccordionContent content;

        public TitleJPanel(AccordionContent content) {
            this.content = content;
            
            this.setLayout(new GridLayout(1, 1));
            JLabel label = new JLabel(content.title);
            label.setFont(label.getFont().deriveFont(14.0f));
            
            if (displayed.contains(content)) {
                label.setIcon(COLLAPSE_ICON);
                this.setBackground(titleSelected);
            } else {
                label.setIcon(EXPAND_ICON);
                this.setBackground(titleBackground);
            }
            this.add(label);
            
            this.addMouseListener(this);
            this.addMouseMotionListener(this);
        }

        public void mouseClicked(MouseEvent me) {}

        public void mousePressed(MouseEvent me) {}

        public void mouseReleased(MouseEvent me) {
            if (displayMode == DISPLAY_MODE_SINGLE) {
                displayed.clear();
                displayed.add(content);
            } else if (!displayed.contains(content)) {
                displayed.add(content);
            } else {
                displayed.remove(content);
            }
            
            refresh();
        }

        public void mouseEntered(MouseEvent me) {
            this.setBackground(titleHover);
            this.validate();
            this.repaint();
        }

        public void mouseExited(MouseEvent me) {
            if (displayed.contains(content)) {
                this.setBackground(titleSelected);
            } else {
                this.setBackground(titleBackground);
            }
            this.validate();
            this.repaint();
        }

        public void mouseDragged(MouseEvent me) {
            this.setBackground(titleHover);
            this.validate();
            this.repaint();
        }

        public void mouseMoved(MouseEvent me) {
            this.setBackground(titleHover);
            this.validate();
            this.repaint();
        }
        
    }
    
}
